﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;


           

namespace BYManageInfoSystem
{
    public partial class Frm_FinDepRecvAndPayDetailSheet : Form
    {
        private DGBY.BLL.FinAbstractTab m_bllFinAbstractTab = new DGBY.BLL.FinAbstractTab();
        private DGBY.BLL.FinRecvMoneyDetailTab m_bllFinRecvMoneyDetailTab = new DGBY.BLL.FinRecvMoneyDetailTab();
        private DGBY.BLL.FinDepRecvAndPayDetailTab m_bllFinDepRecvAndPayDetailTab = new DGBY.BLL.FinDepRecvAndPayDetailTab();
        private int m_nSelectFinDepRecvAndPayDetailTabID = 0;
        private Dictionary<string, string> dicQueryCondition = new Dictionary<string, string>();

        string m_strQueryWhere = "";
        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
        }

        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }

        public Frm_FinDepRecvAndPayDetailSheet()
        {
            InitializeComponent();
        }

        private void InitCombox()
        {
            DataSet ds = m_bllFinAbstractTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxAbstract.DataSource = ds.Tables[0];
                this.comboBoxAbstract.DisplayMember = "摘要";
                this.comboBoxAbstract.ValueMember = "FinAbstractTabID";
                this.comboBoxAbstract.SelectedIndex = 0;
            }
        }

        private void Frm_FinDepRecvAndPayDetailSheet_Load(object sender, EventArgs e)
        {
            InitCombox();
            LocalPrintPreveiw();
            InitUltraWinGridSetting();
            InitQueryCondition();
        }

        private void ShowAllRecord(string strWhere)
        {
            ultraGrid1.DataSource = null;
            DataSet ds = m_bllFinDepRecvAndPayDetailTab.ShowFinDepRecvAndPayDetailRec(strWhere);

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            }
        }

        private bool IsDecimal(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"([1-9]+[0-9]*(\.[0-9]+))|(0(\.[0-9]+))");
            return bRet;
        }

        private bool IsNum(string strInput)
        {
            bool bRet =Regex.IsMatch(strInput, @"^[1-9]+\d*$");
            return bRet;
        }
        
        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        public bool IsDecimal(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsDecimal(TextBox.Text) && !IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNum(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNull(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.FinDepRecvAndPayDetailTab modelFinDepRecvAndPayDetailTab = new DGBY.Model.FinDepRecvAndPayDetailTab();
                string strSeq = this.textBoxSeqNo.Text;
                int nFinRecvMoneyWayId = 0;
                Decimal dRecvMoney = 0;
                Decimal dPaiJia = 0;
                Decimal dPaiJiaMoney = 0;
                DataSet ds = m_bllFinRecvMoneyDetailTab.GetList("SenquenceNo = '" + strSeq + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strFinRecvMoneyWayId = ds.Tables[0].Rows[0]["FinRecvMoneyDetailTabID"].ToString();
                    nFinRecvMoneyWayId = Convert.ToInt32(strFinRecvMoneyWayId, 10);
                    string strRecvMoney = ds.Tables[0].Rows[0]["RecvMoneyValue"].ToString();
                    dRecvMoney = Convert.ToDecimal(strRecvMoney);
                    dPaiJia = Convert.ToDecimal(ds.Tables[0].Rows[0]["Paijia"].ToString());
                    dPaiJiaMoney = Convert.ToDecimal(ds.Tables[0].Rows[0]["PaijiaJinE"].ToString());
                }
                else
                {
                    MessageBox.Show("收款明细表中没有序号为" + strSeq + "的记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (this.comboBoxAbstract.SelectedValue != null)
                    modelFinDepRecvAndPayDetailTab.FinAbstractTabID = Convert.ToInt32(this.comboBoxAbstract.SelectedValue.ToString(), 10);

                ds = m_bllFinDepRecvAndPayDetailTab.GetList("FinRecvMoneyDetailTabID = '" + nFinRecvMoneyWayId.ToString() + "'"
                                                            + " and FinAbstractTabID = '" + modelFinDepRecvAndPayDetailTab.FinAbstractTabID.ToString() + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("序号为" + strSeq + "且摘要为" + this.comboBoxAbstract.Text + "的记录已经存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                List<string> lstNo = new List<string>();
                ds = m_bllFinDepRecvAndPayDetailTab.GetList("FinRecvMoneyDetailTabID = '" + nFinRecvMoneyWayId.ToString() + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        string strDefaultNo = dr["DefaultNo"].ToString();
                        string[] strNo = strDefaultNo.Split('_');
                        lstNo.Add(strNo[1]);
                    }
                }

                int i = 1;

                for (i = 1; i <= 4; i++)
                {
                    if (!lstNo.Contains(i.ToString()))
                        break;
                }
                //int nNextNo = ds.Tables[0].Rows.Count + 1;
                modelFinDepRecvAndPayDetailTab.DefaultNo = strSeq + "_" + i.ToString();

                modelFinDepRecvAndPayDetailTab.FinRecvMoneyDetailTabID = nFinRecvMoneyWayId;

                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxBGFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxSXFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxLXFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxOtherFee))
                    return;

                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxEsHuiLv))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxAdvanPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxFactor1))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxFactor2))
                    return;

                modelFinDepRecvAndPayDetailTab.BianHao = this.textBoxCode.Text;
                modelFinDepRecvAndPayDetailTab.BGFee = Convert.ToDecimal(this.textBoxBGFee.Text);
                modelFinDepRecvAndPayDetailTab.SXFee = Convert.ToDecimal(this.textBoxSXFee.Text);
                modelFinDepRecvAndPayDetailTab.LXFee = Convert.ToDecimal(this.textBoxLXFee.Text);
                modelFinDepRecvAndPayDetailTab.OtherFee = Convert.ToDecimal(this.textBoxOtherFee.Text);
                modelFinDepRecvAndPayDetailTab.FactRecvMoney = dRecvMoney - modelFinDepRecvAndPayDetailTab.BGFee
                                                                - modelFinDepRecvAndPayDetailTab.SXFee
                                                                - modelFinDepRecvAndPayDetailTab.LXFee
                                                                - modelFinDepRecvAndPayDetailTab.OtherFee;
                modelFinDepRecvAndPayDetailTab.HuiChaMoney = modelFinDepRecvAndPayDetailTab.FactRecvMoney;
                modelFinDepRecvAndPayDetailTab.BackTaxMoney = modelFinDepRecvAndPayDetailTab.FactRecvMoney;
                modelFinDepRecvAndPayDetailTab.HuiLv = dPaiJia;
                modelFinDepRecvAndPayDetailTab.EstimationHuiLv = Convert.ToDecimal(this.textBoxEsHuiLv.Text);
                modelFinDepRecvAndPayDetailTab.YuFuMoney = Convert.ToDecimal(this.textBoxAdvanPay.Text);
                modelFinDepRecvAndPayDetailTab.FactorA = Convert.ToDecimal(this.textBoxFactor1.Text);
                modelFinDepRecvAndPayDetailTab.FactorB = Convert.ToDecimal(this.textBoxFactor2.Text);

                modelFinDepRecvAndPayDetailTab.BFBackTaxMoney = modelFinDepRecvAndPayDetailTab.BackTaxMoney
                                                                * modelFinDepRecvAndPayDetailTab.FactorA
                                                                * modelFinDepRecvAndPayDetailTab.HuiLv;

                modelFinDepRecvAndPayDetailTab.PayHuiChaMoney = modelFinDepRecvAndPayDetailTab.HuiChaMoney
                   * (modelFinDepRecvAndPayDetailTab.HuiLv - modelFinDepRecvAndPayDetailTab.EstimationHuiLv);

                modelFinDepRecvAndPayDetailTab.PayBackTaxMoney = (modelFinDepRecvAndPayDetailTab.HuiLv - modelFinDepRecvAndPayDetailTab.FactorA)
                    * (Decimal)1.17 / modelFinDepRecvAndPayDetailTab.FactorB - dPaiJiaMoney;

                modelFinDepRecvAndPayDetailTab.PayDate = this.dateTimePickerPayDate.Value.Date;
                modelFinDepRecvAndPayDetailTab.PayMonth = modelFinDepRecvAndPayDetailTab.PayDate.Value.Month;

                string strTemp = this.comboBoxAbstract.Text;

                if (strTemp == "收款_本金")
                {
                    modelFinDepRecvAndPayDetailTab.PriceTaxSum = dPaiJiaMoney;
                }
                else if (strTemp == "收款_退税")
                {
                    modelFinDepRecvAndPayDetailTab.PriceTaxSum = modelFinDepRecvAndPayDetailTab.PayBackTaxMoney;
                }
                else if (strTemp == "收款_汇差")
                {
                    modelFinDepRecvAndPayDetailTab.PriceTaxSum = modelFinDepRecvAndPayDetailTab.PayHuiChaMoney;
                }
                else
                {
                    modelFinDepRecvAndPayDetailTab.PriceTaxSum = dPaiJiaMoney + modelFinDepRecvAndPayDetailTab.PayBackTaxMoney;
                }


                m_bllFinDepRecvAndPayDetailTab.Add(modelFinDepRecvAndPayDetailTab);
                ShowAllRecord("");
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
      
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.FinDepRecvAndPayDetailTab modelFinDepRecvAndPayDetailTabOld = m_bllFinDepRecvAndPayDetailTab.GetModel(m_nSelectFinDepRecvAndPayDetailTabID);
                if (modelFinDepRecvAndPayDetailTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.FinDepRecvAndPayDetailTab modelFinDepRecvAndPayDetailTab = new DGBY.Model.FinDepRecvAndPayDetailTab();
                modelFinDepRecvAndPayDetailTab.FinDepRecvAndPayDetailTabID = modelFinDepRecvAndPayDetailTabOld.FinDepRecvAndPayDetailTabID;
                string strSeq = this.textBoxSeqNo.Text;
                int nFinRecvMoneyWayId = 0;
                Decimal dRecvMoney = 0;
                Decimal dPaiJia = 0;
                Decimal dPaiJiaMoney = 0;
                DataSet ds = m_bllFinRecvMoneyDetailTab.GetList("SenquenceNo = '" + strSeq + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strFinRecvMoneyWayId = ds.Tables[0].Rows[0]["FinRecvMoneyDetailTabID"].ToString();
                    nFinRecvMoneyWayId = Convert.ToInt32(strFinRecvMoneyWayId, 10);
                    string strRecvMoney = ds.Tables[0].Rows[0]["RecvMoneyValue"].ToString();
                    dRecvMoney = Convert.ToDecimal(strRecvMoney);
                    dPaiJia = Convert.ToDecimal(ds.Tables[0].Rows[0]["Paijia"].ToString());
                    dPaiJiaMoney = Convert.ToDecimal(ds.Tables[0].Rows[0]["PaijiaJinE"].ToString());
                }
                else
                {
                    MessageBox.Show("收款明细表中没有序号为" + strSeq + "的记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (this.comboBoxAbstract.SelectedValue != null)
                    modelFinDepRecvAndPayDetailTab.FinAbstractTabID = Convert.ToInt32(this.comboBoxAbstract.SelectedValue.ToString(), 10);

                if (modelFinDepRecvAndPayDetailTab.FinAbstractTabID != modelFinDepRecvAndPayDetailTabOld.FinAbstractTabID)
                {
                    ds = m_bllFinDepRecvAndPayDetailTab.GetList("FinRecvMoneyDetailTabID = '" + nFinRecvMoneyWayId.ToString() + "'"
                                                                + " and FinAbstractTabID = '" + modelFinDepRecvAndPayDetailTab.FinAbstractTabID.ToString() + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        MessageBox.Show("序号为" + strSeq + "且摘要为" + this.comboBoxAbstract.Text + "的记录已经存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }

                List<string> lstNo = new List<string>();
                ds = m_bllFinDepRecvAndPayDetailTab.GetList("FinRecvMoneyDetailTabID = '" + nFinRecvMoneyWayId.ToString() + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        string strDefaultNo = dr["DefaultNo"].ToString();
                        string[] strNo = strDefaultNo.Split('_');
                        lstNo.Add(strNo[1]);
                    }
                }

                int i = 1;

                for (i = 1; i <= 4; i++)
                {
                    if (!lstNo.Contains(i.ToString()))
                        break;
                }
                //int nNextNo = ds.Tables[0].Rows.Count + 1;
                modelFinDepRecvAndPayDetailTab.DefaultNo = strSeq + "_" + i.ToString();

                modelFinDepRecvAndPayDetailTab.FinRecvMoneyDetailTabID = nFinRecvMoneyWayId;

                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxBGFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxSXFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxLXFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxOtherFee))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxEsHuiLv))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxAdvanPay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxFactor1))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxFactor2))
                    return;
                modelFinDepRecvAndPayDetailTab.BianHao = this.textBoxCode.Text;
                modelFinDepRecvAndPayDetailTab.BGFee = Convert.ToDecimal(this.textBoxBGFee.Text);
                modelFinDepRecvAndPayDetailTab.SXFee = Convert.ToDecimal(this.textBoxSXFee.Text);
                modelFinDepRecvAndPayDetailTab.LXFee = Convert.ToDecimal(this.textBoxLXFee.Text);
                modelFinDepRecvAndPayDetailTab.OtherFee = Convert.ToDecimal(this.textBoxOtherFee.Text);

                modelFinDepRecvAndPayDetailTab.FactRecvMoney = Convert.ToDecimal(this.textBoxRecvMoneyFact.Text);
                modelFinDepRecvAndPayDetailTab.HuiChaMoney = Convert.ToDecimal(this.textBoxHuiChaJinE.Text);
                modelFinDepRecvAndPayDetailTab.BackTaxMoney = Convert.ToDecimal(this.textBoxBackTaxJinE.Text);
                modelFinDepRecvAndPayDetailTab.HuiLv = Convert.ToDecimal(this.textBoxHuiLv.Text);

                modelFinDepRecvAndPayDetailTab.EstimationHuiLv = Convert.ToDecimal(this.textBoxEsHuiLv.Text);
                modelFinDepRecvAndPayDetailTab.YuFuMoney = Convert.ToDecimal(this.textBoxAdvanPay.Text);
                modelFinDepRecvAndPayDetailTab.FactorA = Convert.ToDecimal(this.textBoxFactor1.Text);
                modelFinDepRecvAndPayDetailTab.FactorB = Convert.ToDecimal(this.textBoxFactor2.Text);

                modelFinDepRecvAndPayDetailTab.BFBackTaxMoney = modelFinDepRecvAndPayDetailTab.BackTaxMoney
                                                                * modelFinDepRecvAndPayDetailTab.FactorA
                                                                * modelFinDepRecvAndPayDetailTab.HuiLv;

                modelFinDepRecvAndPayDetailTab.PayHuiChaMoney = modelFinDepRecvAndPayDetailTab.HuiChaMoney
                   * (modelFinDepRecvAndPayDetailTab.HuiLv - modelFinDepRecvAndPayDetailTab.EstimationHuiLv);

                modelFinDepRecvAndPayDetailTab.PayBackTaxMoney = (modelFinDepRecvAndPayDetailTab.HuiLv - modelFinDepRecvAndPayDetailTab.FactorA)
                    * (Decimal)1.17 / modelFinDepRecvAndPayDetailTab.FactorB - dPaiJiaMoney;

                modelFinDepRecvAndPayDetailTab.PayDate = this.dateTimePickerPayDate.Value.Date;
                modelFinDepRecvAndPayDetailTab.PayMonth = modelFinDepRecvAndPayDetailTab.PayDate.Value.Month;

                string strTemp = this.comboBoxAbstract.Text;

                if (strTemp == "收款_本金")
                {
                    modelFinDepRecvAndPayDetailTab.PriceTaxSum = dPaiJiaMoney;
                }
                else if (strTemp == "收款_退税")
                {
                    modelFinDepRecvAndPayDetailTab.PriceTaxSum = modelFinDepRecvAndPayDetailTab.PayBackTaxMoney;
                }
                else if (strTemp == "收款_汇差")
                {
                    modelFinDepRecvAndPayDetailTab.PriceTaxSum = modelFinDepRecvAndPayDetailTab.PayHuiChaMoney;
                }
                else
                {
                    modelFinDepRecvAndPayDetailTab.PriceTaxSum = dPaiJiaMoney + modelFinDepRecvAndPayDetailTab.PayBackTaxMoney;
                }


                m_bllFinDepRecvAndPayDetailTab.Update(modelFinDepRecvAndPayDetailTab);
                ShowAllRecord("");
                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
         
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            DGBY.Model.FinDepRecvAndPayDetailTab modelFinDepRecvAndPayDetailTabOld = m_bllFinDepRecvAndPayDetailTab.GetModel(m_nSelectFinDepRecvAndPayDetailTabID);
            if (modelFinDepRecvAndPayDetailTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllFinDepRecvAndPayDetailTab.Delete(m_nSelectFinDepRecvAndPayDetailTabID);

                ShowAllRecord("");
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.textBoxSeqNo.Text = "";
                this.textBoxDefaultNo.Text = "";
                this.textBoxCode.Text = "";
                this.textBoxBGFee.Text = "";
                this.textBoxSXFee.Text = "";
                this.textBoxLXFee.Text = "";
                this.textBoxOtherFee.Text = "";
                this.textBoxRecvMoneyFact.Text = "";
                this.textBoxHuiChaJinE.Text = "";
                this.textBoxBackTaxJinE.Text = "";
                this.textBoxHuiLv.Text = "";
                this.textBoxEsHuiLv.Text = "";
                this.textBoxAdvanPay.Text = "";
                this.textBoxFactor1.Text = "";
                this.textBoxFactor2.Text = "";
                this.textBoxBuPayBackTaxMoney.Text = "";
                this.textBoxPayHuiChaMoney.Text = "";
                this.textBoxPriceTaxSum.Text = "";
                this.textBoxPayMonth.Text = "";

                return;
            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
                ugc.PerformAutoResize(PerformAutoSizeType.AllRowsInBand);
                //ugc.PerformAutoResize();
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            string strFinDepRecvAndPayDetailTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectFinDepRecvAndPayDetailTabID = Convert.ToInt32(strFinDepRecvAndPayDetailTabID, 10);
            cellFinDepRecvAndPayDetailTabClick();
        }


        private void cellFinDepRecvAndPayDetailTabClick()
        {
            DGBY.Model.FinDepRecvAndPayDetailTab modelFinDepRecvAndPayDetailTab = m_bllFinDepRecvAndPayDetailTab.GetModel(m_nSelectFinDepRecvAndPayDetailTabID);
            if (modelFinDepRecvAndPayDetailTab != null)
            {
                this.textBoxSeqNo.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["序号"].Text;
                this.textBoxDefaultNo.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["默认序号"].Text;
                this.textBoxCode.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["编号"].Text;

                this.textBoxBGFee.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["报关费"].Text;
                this.textBoxSXFee.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["手续费"].Text;
                this.textBoxLXFee.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["利息费"].Text;
                this.textBoxOtherFee.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["其他费"].Text;
                this.textBoxRecvMoneyFact.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["实收金额"].Text;
                this.textBoxHuiChaJinE.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["汇差金额"].Text;
                this.textBoxBackTaxJinE.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["退税金额"].Text;

                this.textBoxHuiLv.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["汇率"].Text;
                this.textBoxEsHuiLv.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["估计汇率"].Text;
                this.textBoxAdvanPay.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["预付款"].Text;

                this.textBoxFactor1.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["系数A"].Text;
                this.textBoxFactor2.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["系数B"].Text;
                this.textBoxBuPayBackTaxMoney.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["补付退税款"].Text;

                this.textBoxPayHuiChaMoney.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["付汇差金额"].Text;
                this.textBoxPayBackTaxMoney.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["付退税款"].Text;
                this.textBoxPriceTaxSum.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["价税合计"].Text;

                this.textBoxPayMonth.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["付款月份"].Text;

                this.dateTimePickerPayDate.Value = (DateTime)this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["付款日期"].Value;

                this.comboBoxAbstract.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["摘要"].Text;

            }
        }

        private void buttonExport2Excel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                    
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
                ugc.Width++;
            //this.ultraGridPrintDocument1.FitWidthToPages = 1;
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }

        private void InitQueryCondition()
        {
            this.comboBoxLogic.SelectedIndex = 0;
            this.comboBoxQueryCondition.SelectedIndex = 0;
            this.radioButtonValueIs.Select();
            dicQueryCondition.Add("收款日期", "FinRecvMoneyDetailTab.RecvMoneyDate");
            dicQueryCondition.Add("付款日期", "FinDepRecvAndPayDetailTab.PayDate");
        }
        private void StartQuery(bool bIsQueryInRes)
        {
            string strWhere = "";

            string strTemp = this.comboBoxQueryCondition.SelectedItem.ToString();
            if (this.radioButtonBetween.Checked)
            {

                if (strTemp == "收款日期" || strTemp == "付款日期")
                {
                    if (strTemp == "付款日期" || strTemp == "收款日期")
                    {
                        if (!ValidDate(this.textBoxFrom.Text) || !ValidDate(this.textBoxTo.Text))
                        {
                            MessageBox.Show("日期格式有误，正确的格式如 20100808 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                        if (this.textBoxFrom.Text != "" && this.textBoxTo.Text != "")
                        {
                            strWhere = "CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) >= " + "'" + this.textBoxFrom.Text + "'" + " and CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) <= " + "'" + this.textBoxTo.Text + "'";
                        }
                        else
                        {
                            MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    else
                    {
                        if (this.textBoxFrom.Text != "" && this.textBoxTo.Text != "")
                        {
                            strWhere = dicQueryCondition[strTemp] + " BETWEEN " + "'" + this.textBoxFrom.Text + "'" + " AND " + "'" + this.textBoxTo.Text + "'";
                        }
                        else
                        {
                            MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                if (strTemp == "付款日期" || strTemp == "收款日期")
                {
                    if (this.textBoxValueIs.Text != "")
                    {
                        if (!ValidDate(this.textBoxValueIs.Text))
                        {
                            MessageBox.Show("日期格式有误，正确的格式如 20100808 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        strWhere = this.textBoxValueIs.Text + " LIKE CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) ";
                    }
                    else
                    {
                        MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else
                {
                    strWhere = dicQueryCondition[strTemp] + " LIKE " + "'%" + this.textBoxValueIs.Text + "%'";
                }
            }

            if (bIsQueryInRes == false)
            {
                m_strQueryWhere = strWhere;
            }
            else
            {
                if (m_strQueryWhere != "")
                {
                    if (this.comboBoxLogic.SelectedIndex == 0)
                    {
                        m_strQueryWhere = "(" + m_strQueryWhere + " AND " + strWhere + ")";
                    }
                    else if (this.comboBoxLogic.SelectedIndex == 1)
                    {
                        m_strQueryWhere = "(" + m_strQueryWhere + " OR " + strWhere + ")";
                    }
                    else
                    {
                        MessageBox.Show("查询条件有误，请选择\"并且\"或\"或者\"!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                }
                else
                {
                    MessageBox.Show("没有所基于的查询结果，请首先单据\"查询\"按键!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

            }

            ShowAllRecord(m_strQueryWhere);
        }
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            StartQuery(false);
        }

        private void buttonQueryFromResult_Click(object sender, EventArgs e)
        {
            StartQuery(true);
        }

        private void radioButtonValueIs_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonValueIs.Checked)
            {
                this.textBoxFrom.Text = "";
                this.textBoxTo.Text = "";
            }
        }

        private void radioButtonBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonBetween.Checked)
                this.textBoxValueIs.Text = "";
        }


    }
}